#include<iostream>
using namespace std;
int main(){
    int a, b;
    while(cin>>a>>b&&1<=a<=10000&&1<=b<=10000){
        if(a==0&&b==0){
            break;
        }
        int s=1;
        for (int i = 0; i < b; i++)
		{
			s = (s * a) % 1000;
		}
        cout<<s<<endl;
    }
}